package com.competitions.easydubbing.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.competitions.easydubbing.dao.UserInfoDao;
import com.competitions.easydubbing.entity.Result;
import com.competitions.easydubbing.entity.UserInfo;
import com.competitions.easydubbing.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author Mapler
 * @since 2021-5-20
 */
@Service
public class UserServiceImpl implements UserService {
    private UserInfoDao userInfoDao;

    @Autowired
    public UserServiceImpl(UserInfoDao userInfoDao) {
        this.userInfoDao = userInfoDao;
    }

    @Override
    public ResponseEntity<Result> register(JSONObject data) {
        if(!data.containsKey("phone")){
            return ResponseEntity.status(412).body(Result.error("缺少phone参数"));
        }
        if(!data.containsKey("password")){
            return ResponseEntity.status(412).body(Result.error("缺少password参数"));
        }
        if(userInfoDao.selectUserByPhone(data.getString("phone"))!=null){
            return ResponseEntity.status(403).body(Result.error("该账户已被注册"));
        }
        userInfoDao.insertUser(data.getString("phone"),data.getString("password"));
        return ResponseEntity.ok().body(Result.ok());
    }

    @Override
    public ResponseEntity<Result> login(JSONObject data) {
        if(!data.containsKey("phone")){
            return ResponseEntity.status(412).body(Result.error("缺少phone参数"));
        }
        if(!data.containsKey("password")){
            return ResponseEntity.status(412).body(Result.error("缺少password参数"));
        }
        UserInfo userInfo = userInfoDao.selectUser(data.getString("phone"),data.getString("password"));
        if(userInfo == null){
            return ResponseEntity.status(401).body(Result.error("用户名或密码错误"));
        }
        return ResponseEntity.ok().body(Result.ok(userInfo));
    }

    @Override
    public ResponseEntity<Result> findUserInfo(JSONObject data) {
        if(!data.containsKey("uid")){
            return ResponseEntity.status(412).body(Result.error("缺少uid参数"));
        }
        UserInfo userInfo = userInfoDao.selectUserById(data.getInteger("uid"));
        return ResponseEntity.ok().body(Result.ok(userInfo));
    }

    @Override
    public ResponseEntity<Result> updateUserData(JSONObject data) {
        try {
            if(!data.containsKey("uid")){
                return ResponseEntity.status(412).body(Result.error("缺少uid参数"));
            }
            if(!data.containsKey("alias")){
                return ResponseEntity.status(412).body(Result.error("缺少alias参数"));
            }
            if(!data.containsKey("desc")){
                return ResponseEntity.status(412).body(Result.error("缺少desc参数"));
            }
            userInfoDao.updateUserData(
                    data.getInteger("uid"),
                    data.getString("alias"),
                    data.getString("desc"));
            return ResponseEntity.ok().body(Result.ok());
        }catch (Exception e) {
            return ResponseEntity.status(500).body(Result.error("修改失败"));
        }
    }

    @Override
    public ResponseEntity<Result> updateAvatar(int uid, MultipartFile multipartFile) {
        try {
            final String basePath = "C:\\uploadFiles\\";
            String path = basePath+"images\\"+System.currentTimeMillis()+".png";
            File file = new File(path);
            multipartFile.transferTo(file);
            String url = path.replace("C:\\uploadFiles\\images\\","http://47.102.150.68:8066/upload/images/");
            userInfoDao.updateAvatar(uid, url);
            return ResponseEntity.ok().body(Result.ok(url));
        }catch (Exception e) {
            return ResponseEntity.status(500).body(Result.error("修改失败"));
        }
    }
}
